{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_r843k7j"
   },
   "source": [
    "# Constants\n",
    "\n",
    "This example highlights how to use `const` to promise not to modify a variable, even though the variable can only be evaluated at run time.\n",
    "\n",
    "The example also show how to use `constexpr` to guarantee that a variable can be evaluated at compile time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_czkjnvo",
    "graffitiConfig": {
     "executeCellViaGraffiti": "9daoia7_p5h1y8n"
    }
   },
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "\n",
    "int main()\n",
    "{\n",
    "    int i;\n",
    "    std::cout << \"Enter an integer value for i: \";\n",
    "    std::cin >> i;\n",
    "    const int j = i * 2;  // \"j can only be evaluated at run time.\"\n",
    "                          // \"But I promise not to change it after it is initialized.\"\n",
    "    \n",
    "    constexpr int k = 3;  // \"k, in contrast, can be evaluated at compile time.\"\n",
    "    \n",
    "    std::cout << \"j = \" << j << \"\\n\";\n",
    "    std::cout << \"k = \" << k << \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_9daoia7"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_9daoia7-id_p5h1y8n\"><i></i><button>Compile & Run</button></span>&nbsp;\n",
    "<span class=\"graffiti-highlight graffiti-id_d85518f-id_kg221qc\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_nau33ya",
    "graffitiConfig": {
     "rows": 6,
     "terminalId": "id_ujtlugg",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_nau33ya), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_hzyfm2o"
   },
   "source": [
    "The compiler will catch a `const` variable that changes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_mdc5rul",
    "graffitiConfig": {
     "executeCellViaGraffiti": "zpsfp04_vd4pmdf"
    }
   },
   "outputs": [],
   "source": [
    "int main()\n",
    "{\n",
    "    const int i = 2; // \"I promise not to change this.\"\n",
    "    i++;             // \"I just broke my promise.\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_zpsfp04"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_zpsfp04-id_vd4pmdf\"><i></i><button>Compile & Run</button></span>&nbsp;\n",
    "<span class=\"graffiti-highlight graffiti-id_yjqwwkk-id_eetvyh0\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_lxyl40r",
    "graffitiConfig": {
     "rows": 6,
     "terminalId": "id_vdbi7vb",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_lxyl40r), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_o8apmhd"
   },
   "source": [
    "Similarly, the compiler will catch a `constexpr` variable that changes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_vwm5y93",
    "graffitiConfig": {
     "executeCellViaGraffiti": "6cm8alc_ampzw92"
    }
   },
   "outputs": [],
   "source": [
    "int main()\n",
    "{\n",
    "    constexpr int i = 2;  // \"i can be evaluated at compile time.\"\n",
    "    i++;                  // \"But changing a constexpr variable triggers an error.\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_6cm8alc"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_6cm8alc-id_ampzw92\"><i></i><button>Compile & Run</button></span>&nbsp;\n",
    "<span class=\"graffiti-highlight graffiti-id_pxb79hu-id_jldwnpn\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_ngddbd2",
    "graffitiConfig": {
     "rows": 6,
     "terminalId": "id_bxccdav",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_ngddbd2), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_j1tbt1a"
   },
   "source": [
    "The major difference between `const` and `constexpr`, though, is that `constexpr` must be evaluated at compile time.\n",
    "\n",
    "The compiler will catch a `constexpr` variable that cannot be evaluated at compile time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_ljgezkm",
    "graffitiConfig": {
     "executeCellViaGraffiti": "oc4cka0_6bvce88"
    }
   },
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "\n",
    "int main()\n",
    "{\n",
    "    int i;\n",
    "    std::cout << \"Enter an integer value for i: \";\n",
    "    std::cin >> i;\n",
    "    constexpr int j = i * 2;  // \"j can only be evaluated at run time.\"\n",
    "                              // \"constexpr must be evaluated at compile time.\"\n",
    "                              // \"So this code will produce a compilation error.\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_oc4cka0"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_oc4cka0-id_6bvce88\"><i></i><button>Compile & Run</button></span>&nbsp;\n",
    "<span class=\"graffiti-highlight graffiti-id_i3xlazq-id_z1wopru\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_6207vcc",
    "graffitiConfig": {
     "rows": 10,
     "terminalId": "id_vh2ez34",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_6207vcc), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_49699vw"
   },
   "source": [
    "A common usage of `const` is to guard against accidentally changing a variable, especially when it is passed-by-reference as a function argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_fkbtwee",
    "graffitiConfig": {
     "executeCellViaGraffiti": "q3f958n_imkk2gs"
    }
   },
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "#include <vector>\n",
    "\n",
    "int sum(const std::vector<int> &v)\n",
    "{\n",
    "    int sum = 0;\n",
    "    for(int i : v)\n",
    "        sum += i;\n",
    "    return sum;\n",
    "}\n",
    "\n",
    "int main()\n",
    "{\n",
    "    std::vector<int> v {0, 1, 2, 3, 4};\n",
    "    std::cout << sum(v) << \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_q3f958n"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_q3f958n-id_imkk2gs\"><i></i><button>Compile & Run</button></span>&nbsp;\n",
    "<span class=\"graffiti-highlight graffiti-id_2jbu560-id_kmtghge\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_x6tnb0d",
    "graffitiConfig": {
     "rows": 12,
     "terminalId": "id_iygboj9",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_x6tnb0d), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_st9b2ev"
   },
   "source": [
    "The distinction between `const` and `constexpr` is subtle. \n",
    "\n",
    "In general, though, `const` is much more common than `constexpr`.\n",
    "\n",
    "When in doubt, use `const`, especially to guard against accidentally modifying a variable."
   ]
  }
 ],
 "metadata": {
  "graffiti": {
   "firstAuthorId": "8802670962",
   "id": "id_zyhsveg",
   "language": "EN"
  },
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xeus-cling-cpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "-std=c++17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
